package com.bleeth.kettle;

import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.config.SaTokenConfig;
import cn.dev33.satoken.config.SaTokenConfigFactory;
import cn.dev33.satoken.servlet.model.SaStorageForServlet;
import com.bleeth.kettle.config.AuthInterceptor;
import com.bleeth.kettle.controller.DictHandler;
import com.bleeth.kettle.controller.FunctionHandler;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.plugin.cron4j.Cron4jPlugin;
import com.jfinal.server.undertow.UndertowServer;
import com.jfinal.template.Engine;

/**
 * @author BleethNie
 * @version 1.0
 * @date 2021-06-21 11:35
 **/
public class KettleWebServer extends JFinalConfig {

    public static Cron4jPlugin cronPlugin;


    public static void main(String[] args) {
        SaTokenConfig abcConfig = SaManager.getConfig();
        abcConfig.setTokenName("nice");
        abcConfig.setIsLog(true);
        SaTokenConfigFactory saTokenContextForThreadLocal = new SaStorageForServlet();
        SaManager.setSaTokenContext(saTokenContextForThreadLocal);

        UndertowServer
                .create(KettleWebServer.class)
                .config(config -> {
                    config.setHost("127.0.0.1");
                    config.setSessionTimeout(30 * 60);
                    config.setDevMode(true);
                    config.setPort(80);
//                    config.setPort(SysUtil.getRandomPort());
                })
                .start();
        System.err.println("http://127.0.0.1:80/func/test");
    }

    @Override
    public void configConstant(Constants me) {

        me.setDevMode(true);
        me.setToSlf4jLogFactory();
        // 配置 404、500 页面
//        me.setError404View("/common/404.html");
//        me.setError500View("/common/500.html");
    }

    @Override
    public void configRoute(Routes me) {
        me.add("/dict", DictHandler.class);
        me.add("/func", FunctionHandler.class);

    }

    @Override
    public void configEngine(Engine me) {

    }

    @Override
    public void configPlugin(Plugins me) {

    }

    @Override
    public void configInterceptor(Interceptors me) {
        AuthInterceptor authInterceptor = new AuthInterceptor();
        me.addGlobalActionInterceptor(authInterceptor);

    }

    @Override
    public void configHandler(Handlers me) {

    }
}
